<?php


class ToDatabase{

	
	/**
	$member = array();
	$member['openid'] = 'os9qxtydN4HgcBTBX9AStCkvpOiU';
	$member['phone'] = '13687177529';
	$member['name'] = '年后';
	**/
	public function queryById($open_id){
		//$open_id 获得的微信openi
		$member = array();
		include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
		try {
		$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
		//echo "连接成功<br/>";
		//你还可以进行一次搜索操作
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "SELECT * FROM `members` WHERE `openId` LIKE '".$open_id."'";
		$result = $dbh->query($sql);
		//var_dump($result);
		foreach ($result as $row) {
			//print_r($row["zan"]); //你可以用 echo($GLOBAL); 来看到这些值
			$member = $row;
			//var_dump($member);
		}
		
		$dbh = null;
	} catch (PDOException $e) {
		die ("Error!: " . $e->getMessage() . "<br/>");
	}
		
		return $member;
		
	}
	
	
	
	
	public function queryMember($member){
			// $member phone

		try {
				include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
				$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
				//echo "连接成功<br/>";
				//你还可以进行一次搜索操作
				$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
				$sql = "SELECT * FROM `members` WHERE `phone` LIKE  '".$member['phone']."'";
				foreach ($dbh->query($sql) as $row) {
					//print_r($row["zan"]); //你可以用 echo($GLOBAL); 来看到这些值
					$member["phone"] = $row["phone"];
					$member["name"] = $row["name"];
					$member["free"] =$row["free"];
					$member["openid2"] = $row["openId"];
				}
				
				$dbh = null;
			} catch (PDOException $e) {
				die ("Error!: " . $e->getMessage() . "<br/>");
			}

			var_dump($member);


	}

	public function uoiMember($member){
		
		//$member openid phone name openId
		//插入会员记录,首先更新，如未更新成功，就是数据完整存在，和 数据完全不存在
		try {
			include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
			$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
			//echo "连接成功<br/>";
			//你还可以进行一次搜索操作
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "UPDATE `members` SET `openId`='".$member['openid']."' WHERE `phone` LIKE '".$member['phone']."'";
			$count = $dbh->exec($sql);
			if($count == 0){
				$sql = "INSERT INTO `members` (`id`, `name`, `gender`, `birthday`, `calendar`, `main`, `phone`, `joinshop`, `jointime`, `card`, `cardkind`, `cardpass`, `starttime`, `endtime`, `money`, `freemoney`, `points`, `amount`, `kindproof`, `proofnumber`, `email`, `apeartment`, `free`, `address`, `wechat`, `openId`, `back`) VALUES (NULL, '".$member['name']."', '', '', '', '', '".$member['phone']."', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '0', '', '', '".$member['openid']."', '')";
				$count = $dbh->exec($sql);
				//var_dump($count);
			};
			
			$dbh = null;
		} catch (PDOException $e) {
			die ("Error!: " . $e->getMessage() . "<br/>");
		}
		return $count;
		
	}
		

	public function insertDishs($dish){	
	
		/**
		$dish['phone'],  赠送菜品给的会员的手机号
		$dish['name'],赠送的菜品名
		$dish['desk'];赠送的桌号
		$dish['time'],赠送的时间
		Sdish['openid'] 赠送菜使用人的openid
		**/
		//插入赠送记录
		try {
			include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
			$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
			//echo "连接成功<br/>";
			//你还可以进行一次搜索操作
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "INSERT INTO `freedish` (`id`, `phone`, `dish`,`desk`, `time`) VALUES (NULL, '".$dish['phone']."', '".$dish['name']."','".$dish['desk']."', '".$dish['time']."')";
			$count = $dbh->exec($sql);
			
			$sql2 = "UPDATE `members` SET `free` = '1' WHERE `members`.`openId` = '".$dish['openid']."'";
			$dbh->exec($sql2);
			
			$dbh = null;
		} catch (PDOException $e) {
			die ("Error!: " . $e->getMessage() . "<br/>");
		}
		
		//返回插入成功与否信息，0为没有插入，1为成功影响一行
		return $count;
		
	}

		//插入预定记录
	public function insertReservation($reservation){
		/**
		$reservation['phone'] //预定人的手机号
		$reservation['name'] // 预定人的名字
		$reservation['date'] //预定日期
		$reservation['time']  //预定的时间
		$reservation['desk']  //预定的桌号
		$reservation['dish']	//会员特权菜品
		$reservation['bakeup'] //备注的要求
		$reservation['openid'] //预定openid
		**/
		//var_dump($reservation);
		//插入预定记录
		try {
			include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
			$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
			//echo "连接成功<br/>";
			//你还可以进行一次搜索操作
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "INSERT INTO `reservation` (`id`, `phone`, `name`, `date`, `time`, `desk`,`dish`, `backup`) VALUES (NULL, '".$reservation['phone']."', '".$reservation['name']."', '".$reservation['date']."', '".$reservation['time']."', '".$reservation['desk']."', '".$reservation['dish']."', '".$reservation['backup']."')";
			$count = $dbh->exec($sql);
			if(!empty($reservation["dish"])){
					$sql2 = "UPDATE `members` SET `free` = '1' WHERE `members`.`openId` = '".$reservation['openid']."'";
					$dbh->exec($sql2);
			}
			
			
			$dbh = null;
		} catch (PDOException $e) {
			die ("Error!: " . $e->getMessage() . "<br/>");
		}
		
		return $count;
		
		
	}
	//查询赠送菜
	public function queryDishsById($open_id){
		
		//$open_id 获得的微信openid
		if($open_id == ""){
			$open_id = "getfalse";
		}
		$member = array();
		include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
		try {
		$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
		//echo "连接成功<br/>";
		//你还可以进行一次搜索操作
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "SELECT members.name,freedish.phone,freedish.dish,freedish.desk,freedish.time FROM `members` RIGHT JOIN freedish ON members.phone = freedish.phone WHERE members.openId LIKE '".$open_id."'ORDER BY freedish.time DESC";
		$result = $dbh->query($sql);
		//var_dump($result);
		foreach ($result as $row) {
			//print_r($row["zan"]); //你可以用 echo($GLOBAL); 来看到这些值
			$dishs[] = $row;
			//var_dump($dishs);
		}
		
		$dbh = null;
	} catch (PDOException $e) {
		die ("Error!: " . $e->getMessage() . "<br/>");
	}
		
		return $dishs;
		
		
	}
	
	
	public function queryReservationById($open_id){
		
		//$open_id 获得的微信openid
		if($open_id == ""){
			$open_id = "getfalse";
		}
		$member = array();
		include $_SERVER['DOCUMENT_ROOT'].'/online-system/freenew/config.php';
		try {
		$dbh = new PDO($dsn, $user, $pass,$charset); //初始化一个PDO对象
		//echo "连接成功<br/>";
		//你还可以进行一次搜索操作
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "SELECT reservation.date,reservation.time,reservation.desk,reservation.dish,reservation.backup FROM `members` RIGHT JOIN reservation ON members.phone = reservation.phone WHERE members.openId LIKE '".$open_id."' ORDER BY reservation.date DESC";
		$result = $dbh->query($sql);
		//var_dump($result);
		foreach ($result as $row) {
			//print_r($row["zan"]); //你可以用 echo($GLOBAL); 来看到这些值
			$reservations[] = $row;
			//var_dump($member);
		}
		
		$dbh = null;
	} catch (PDOException $e) {
		die ("Error!: " . $e->getMessage() . "<br/>");
	}
		
		return $reservations;
		
		
	}
		
	
}	
	
	
	
?>